package com.crm.eg.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.crm.eg.bean.UserAccount;
import com.crm.eg.dao.UserAccountDAO;
import com.crm.exception.DBException;

/**
 * Data access object (DAO) for domain model class UserAccount.
 * 
 * @see com.bean.UserAccount
 * @author MyEclipse Persistence Tools
 */

public class UserAccountDAOImpl extends HibernateDaoSupport implements
		UserAccountDAO {
	private static final Log log = LogFactory.getLog(UserAccountDAO.class);

	// property constants
	public static final String USERNAME = "username";

	public static final String PASSWORD = "password";

	public static final String GROUPNAME = "groupname";

	public void save(UserAccount transientInstance) throws DBException {
		log.debug("saving UserAccount instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (Exception re) {
			log.error("save failed", re);
			throw new DBException(re);
		}
	}

	public void delete(UserAccount persistentInstance) throws DBException {
		log.debug("deleting UserAccount instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (Exception re) {
			log.error("delete failed", re);
			throw new DBException(re);
		}
	}

	public UserAccount findById(java.lang.Long id) throws DBException {
		log.debug("getting UserAccount instance with id: " + id);
		try {
			UserAccount instance = (UserAccount) getHibernateTemplate().get(
					"com.bean.UserAccount", id);
			return instance;
		} catch (Exception re) {
			log.error("get failed", re);
			throw new DBException(re);
		}
	}

	public List<UserAccount> findByExample(UserAccount instance)
			throws DBException {
		log.debug("finding UserAccount instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (Exception re) {
			log.error("find by example failed", re);
			throw new DBException(re);
		}
	}

	public List<UserAccount> findByProperty(String propertyName, Object value)
			throws DBException {
		log.debug("finding UserAccount instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from UserAccount as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (Exception re) {
			log.error("find by property name failed", re);
			throw new DBException(re);
		}
	}

	public List<UserAccount> findByUsername(Object username) throws DBException {
		try {
			return findByProperty(USERNAME, username);
		} catch (Exception re) {
			log.error("find By Username failed", re);
			throw new DBException(re);
		}

	}

	public List<UserAccount> findByPassword(Object password) throws DBException {

		try {
			return findByProperty(PASSWORD, password);
		} catch (Exception re) {
			log.error("find By Username failed", re);
			throw new DBException(re);
		}
	}

	public List<UserAccount> findByGroupname(Object groupname)
			throws DBException {

		try {
			return findByProperty(GROUPNAME, groupname);
		} catch (Exception re) {
			log.error("find By Username failed", re);
			throw new DBException(re);
		}
	}

	public List<UserAccount> findAll() throws DBException {
		log.debug("finding all UserAccount instances");
		try {
			String queryString = "from UserAccount";
			return getHibernateTemplate().find(queryString);
		} catch (Exception re) {
			log.error("find all failed", re);
			throw new DBException(re);
		}
	}

	public UserAccount merge(UserAccount detachedInstance) throws DBException {
		log.debug("merging UserAccount instance");
		try {
			UserAccount result = (UserAccount) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (Exception re) {
			log.error("merge failed", re);
			throw new DBException(re);
		}
	}

	public void attachDirty(UserAccount instance) throws DBException {
		log.debug("attaching dirty UserAccount instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (Exception re) {
			log.error("attach failed", re);
			throw new DBException(re);
		}
	}

	public void attachClean(UserAccount instance) throws DBException {
		log.debug("attaching clean UserAccount instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (Exception re) {
			log.error("attach failed", re);
			throw new DBException(re);
		}
	}

	public static UserAccountDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (UserAccountDAO) ctx.getBean("userAccountDAO");
	}
}